package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import org.json.JSONException;
import org.json.JSONObject;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public abstract class BaseDownloadFrg extends BaseFragment {
    protected View  m_vTopPanel, m_vWaitPanel;
    protected TextView m_tvError;
    protected View m_vEmptyResult;
    
    private String  m_strErrorMsgTemplate;
    
    private static final String LOG_TAG = BaseDownloadFrg.class.getSimpleName();
    
    ////////////////////////////////////////
    @Override public void 
    onCreate(Bundle bdSavedInstanceState)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreate()");
        super.onCreate(bdSavedInstanceState);
        
        m_strErrorMsgTemplate = getString(R.string.error_msg);
    }    
    
    @Override public void 
    onActivityCreated(Bundle bdSavedInstanceState)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onActivityCreated()");
        super.OnActivityCreated(bdSavedInstanceState, true);
        
        m_vEmptyResult.setVisibility(View.VISIBLE);
        m_vTopPanel.setVisibility(View.INVISIBLE);
        m_vWaitPanel.setVisibility(View.INVISIBLE);
    }
    
    private void
    _ShowWaitPanel(boolean bShowWait)
    {
        if (getActivity() == null) return;
        
        m_vTopPanel.setVisibility(bShowWait ? View.INVISIBLE : View.VISIBLE);
        m_vWaitPanel.setVisibility(bShowWait ? View.VISIBLE : View.INVISIBLE); 
    }
    
    abstract protected boolean IsMyDownloadCmd(int nCmd);

    protected void 
    OnMyDownloadError(int nCmd, String strError)
    {
        if (getView() == null) return;
        
        m_tvError.setVisibility(View.VISIBLE);
        m_tvError.setText(strError);
    }

    protected void 
    OnMyDownloadSuccess(int nCmd, JSONObject json)
    {   
        if (getView() == null) return;
    }

    @Override protected void 
    OnDownloadStart(int nCmd)
    {
        if (!IsMyDownloadCmd(nCmd))
            return;
        if (getView() == null) return;
        
        m_tvError.setVisibility(View.INVISIBLE);
        m_tvError.setText(null);
        
        m_vEmptyResult.setVisibility(View.INVISIBLE);
        
        _ShowWaitPanel(true);
    }

    @Override protected void 
    OnDownloadStop(int nCmd)
    {
        if (!IsMyDownloadCmd(nCmd))
            return;
        if (getView() == null) return;
        
        _ShowWaitPanel(false);
    }

    @Override protected void 
    OnDownloadFinishedError(int nCmd, String strError)
    {
        if (!IsMyDownloadCmd(nCmd))
            return;
        if (getView() == null) return;
        
        OnMyDownloadError(nCmd, strError);
    }

    @Override protected void 
    OnDownloadFinishedOkay(int nCmd, String strResult)
    {
        if (!IsMyDownloadCmd(nCmd))
            return;
        if (getView() == null) return;
        
        try {
            JSONObject json = new JSONObject(strResult);
            
            int nReturnCode = json.getInt("rc");
            if (nReturnCode != 0) {
                OnMyDownloadError(nCmd, 
                                  String.format(m_strErrorMsgTemplate, Integer.toString(nReturnCode)));
                return;
            }
            OnMyDownloadSuccess(nCmd, json);
        }
        catch (JSONException e) {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "", e);
            
            OnMyDownloadError(nCmd, 
                              String.format(m_strErrorMsgTemplate, e.toString()));
        }
    }
}
